import 'package:f200/common/index.dart';
import 'package:f200/models/goods/goods_model.dart';
import 'package:f200/pages/home/controllers/home_view_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class HomeView extends GetView<HomeViewController> {
  const HomeView({super.key});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        AppTitleBar(title: '首页'),
        Expanded(
          child: SingleChildScrollView(
            child: Column(
              children: [
                ADSwiperWidget(height: 120), // 构建广告轮播
                _buildTextIconNav(), // 构建文本图标导航
                _buildBanner(), // 构建广告位
                _buildSecKill(), // 构建秒杀
                _buildPinTuan(), // 构建拼团
                _buildHotSection(context), // 构建热销商品区块
                _buildTopNSection(context), // 构建TOPN商品区块
                _buildBottomSpace(),
              ],
            ),
          ),
        ),
      ],
    );
  }

  /// 构建视图底部空白
  Widget _buildBottomSpace() {
    return SizedBox(height: 60);
  }

  /// 构建TOPN商品区块
  Widget _buildTopNSection(BuildContext context) {
    return Column(
      children: [
        _buildHotSectionTitleBar('店主推荐', '爆款、优惠'),
        Container(
          color: Theme.of(context).colorScheme.background,
          child: Wrap(
            children:
                controller.goodsDb.map((e) => _buildGoodsListItem(e)).toList(),
          ),
        ),
      ],
    );
  }

  /// 构建热销商品区块
  Widget _buildHotSection(BuildContext context) {
    return Column(
      children: [
        _buildHotSectionTitleBar('当季热卖', '新鲜、实惠'),
        Container(
          color: Theme.of(context).colorScheme.background,
          child: Wrap(
            children:
                controller.goodsDb.map((e) => _buildGoodsListItem(e)).toList(),
          ),
        ),
      ],
    );
  }

  /// 构建商品列表单个商品
  Widget _buildGoodsListItem(GoodsModel goods) {
    return Container(
      width: 180,
      padding: EdgeInsets.only(top: 12, left: 6, right: 6),
      color: Colors.white,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          GestureDetector(
            onTap: () {
              Get.toNamed('/goodsDetail',
                  parameters: Map.from({'id': goods.id.toString()}));
            },
            child: Container(
              width: 180,
              height: 200,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(6),
                  color: Colors.grey[350],
                  image: DecorationImage(
                      fit: BoxFit.fill, image: NetworkImage(goods.thumbnail))),
            ),
          ),
          SizedBox(height: 8),
          GestureDetector(
            onTap: () {
              Get.toNamed('/goodsDetail',
                  parameters: Map.from({'id': goods.id.toString()}));
            },
            child: Text(goods.title,
                style: TextStyle(overflow: TextOverflow.ellipsis)),
          ),
          SizedBox(height: 4),
          Text(goods.subTitle,
              style: TextStyle(fontSize: 12, color: Colors.grey[700])),
          SizedBox(height: 4),
          Row(
            children: [
              ...?goods.salesTag?.map(
                (e) => Container(
                  padding: EdgeInsets.all(2),
                  margin: EdgeInsets.all(2),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(2),
                    color: Colors.grey[50],
                    border: Border.all(color: Colors.redAccent, width: 0.2),
                  ),
                  child: Text(
                    e,
                    style: TextStyle(fontSize: 10, color: Colors.redAccent),
                  ),
                ),
              ),
            ],
          ),
          SizedBox(height: 8),
          Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Text('￥',
                  style: TextStyle(fontSize: 12, color: Colors.redAccent)),
              Text(goods.price.toString(),
                  style: TextStyle(fontSize: 18, color: Colors.redAccent)),
              SizedBox(width: 4),
              Text(goods.originPrice.toString(),
                  style: TextStyle(fontSize: 12, color: Colors.grey)),
            ],
          ),
          SizedBox(height: 4),
          Row(
            children: [
              Text('回头客${goods.repeatCustomer}+',
                  style: TextStyle(fontSize: 10, color: Colors.redAccent)),
              SizedBox(width: 2),
              // Icon(
              //   Icons.star,
              //   size: 12,
              //   color: Colors.redAccent,
              // ),
              // Text('${e.score}', style: TextStyle(fontSize: 12, color: Colors.grey)),
              // SizedBox(width: 6),
              Text('${goods.evaluate}+评论',
                  style: TextStyle(fontSize: 10, color: Colors.grey)),
              SizedBox(width: 2),
              Text('${goods.favourable}%好评',
                  style: TextStyle(fontSize: 10, color: Colors.grey))
            ],
          ),
        ],
      ),
    );
  }

  /// 构建热销商品区块标题栏
  Widget _buildHotSectionTitleBar(String title, String subTitle) {
    return Container(
      height: 40,
      color: Colors.grey[100],
      margin: EdgeInsets.only(top: 12),
      padding: EdgeInsets.symmetric(horizontal: 4, vertical: 8),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Expanded(
              child: Text(title,
                  style: TextStyle(
                      color: Color(0xFFff3861),
                      fontSize: 16,
                      fontWeight: FontWeight.bold))),
          Text(subTitle,
              style: TextStyle(fontSize: 12, color: Colors.grey[800])),
          Icon(Icons.keyboard_arrow_right, size: 14),
        ],
      ),
    );
  }

  /// 构建拼团区块
  Widget _buildPinTuan() {
    return Card(
      margin: EdgeInsets.symmetric(vertical: 8, horizontal: 4),
      elevation: 0,
      child: Column(
        children: [
          _buildSectionTitleBar('超值拼团'),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _buildSecKillItem(
                '克伦生无籽红提 500g/份',
                'assets/icons/icon.jpg',
                8.90,
              ),
              _buildSecKillItem(
                '克伦生无籽红提 500g/份',
                'assets/icons/icon.jpg',
                8.90,
              ),
              _buildSecKillItem(
                '克伦生无籽红提 500g/份',
                'assets/icons/icon.jpg',
                8.90,
              ),
            ],
          ),
        ],
      ),
    );
  }

  /// 构建限时秒杀区块
  Widget _buildSecKill() {
    return Card(
      margin: EdgeInsets.symmetric(vertical: 8, horizontal: 4),
      elevation: 0,
      child: Column(
        children: [
          _buildSectionTitleBar('限时秒杀'),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _buildSecKillItem(
                '克伦生无籽红提 500g/份',
                'assets/icons/icon.jpg',
                8.90,
              ),
              _buildSecKillItem(
                '克伦生无籽红提 500g/份',
                'assets/icons/icon.jpg',
                8.90,
              ),
              _buildSecKillItem(
                '克伦生无籽红提 500g/份',
                'assets/icons/icon.jpg',
                8.90,
              ),
            ],
          ),
        ],
      ),
    );
  }

  /// 构建区块标题栏
  Widget _buildSectionTitleBar(String title) {
    return Container(
      height: 40,
      color: Colors.grey[100],
      padding: EdgeInsets.symmetric(horizontal: 4, vertical: 8),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Expanded(
            child: Text(
              title,
              style: TextStyle(
                  color: Color(0xFFff3861),
                  fontSize: 16,
                  fontWeight: FontWeight.bold),
            ),
          ),
          Text('更多', style: TextStyle(fontSize: 12, color: Colors.grey[800])),
          Icon(Icons.keyboard_arrow_right, size: 14),
        ],
      ),
    );
  }

  /// 构建秒杀商品
  Widget _buildSecKillItem(String title, String imageUri, double price) {
    return Container(
      width: 100,
      padding: EdgeInsets.symmetric(vertical: 8),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Image.asset(
            imageUri,
            width: 100,
            height: 80,
            fit: BoxFit.cover,
          ),
          SizedBox(height: 4),
          Text(
            title,
            style: TextStyle(fontSize: 12, color: Colors.grey[800]),
            overflow: TextOverflow.ellipsis,
          ),
          SizedBox(height: 4),
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Text(
                '秒杀价',
                style: TextStyle(color: Colors.red, fontSize: 12),
              ),
              SizedBox(width: 4),
              RichText(
                text: TextSpan(
                  text: '￥',
                  style: TextStyle(color: Colors.red, fontSize: 10),
                  children: [
                    TextSpan(
                      text: '${price}',
                      style: TextStyle(color: Colors.red, fontSize: 16),
                    ),
                  ],
                ),
              )
            ],
          ),
          SizedBox(height: 4),
        ],
      ),
    );
  }

  /// 构建广告位
  Widget _buildBanner() {
    return Container(
      height: 80,
      width: double.maxFinite,
      margin: EdgeInsets.all(8),
      clipBehavior: Clip.hardEdge,
      decoration: BoxDecoration(
        color: Colors.grey,
        borderRadius: BorderRadius.circular(46),
      ),
      child: Image.asset(
        'assets/images/ad1.png',
        height: 80,
        fit: BoxFit.fill,
      ),
    );
  }

  /// 构建文本图标导航
  Widget _buildTextIconNav() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        TextIconNav(
          icon: Icons.eco_outlined,
          text: '积分商城',
        ),
        TextIconNav(
          icon: Icons.local_offer_outlined,
          text: '领券中心',
        ),
        TextIconNav(
          icon: Icons.card_giftcard,
          text: '火爆预售',
        ),
        TextIconNav(
          icon: Icons.checklist_outlined,
          text: '每日签到',
        ),
      ],
    );
  }
}
